from django.conf.urls import url
from django.http import HttpResponse
from django.urls import include, re_path
from django.urls import path
from django.views.static import serve
from rest_framework import routers
from rest_framework_jwt.views import obtain_jwt_token

import xadmin
from city.models import China
from city.views import CityViewSet
from django_app.settings import MEDIA_ROOT
from motion.views import InvolverMotionPathViewSet
from report.views import InvolverInfoListViewSet, MigrantsInfoViewSet, PartyViewSet

router = routers.DefaultRouter()
router.register(r'involverInfos', InvolverInfoListViewSet)
router.register(r'InvolverMotionPaths', InvolverMotionPathViewSet)
router.register(r'MigrantsInfos', MigrantsInfoViewSet)
# router.register(r'involverInfosPartial', InvolverInfoListViewSetPartial)
router.register(r'city', CityViewSet)
router.register(r'Party', PartyViewSet)


def format_code(code):
    s = ""
    if len(code) == 6:
        return code
    for i in range(6 - len(code)):
        s = s + "0"
    return code + s


def fc(request):
    import json
    f1 = open("province.json", encoding='utf-8')
    f2 = open("city.json", encoding='utf-8')
    f3 = open("area.json", encoding='utf-8')
    province_data = json.load(f1)
    city_data = json.load(f2)
    area_data = json.load(f3)
    for i in range(len(province_data)):
        province = province_data[i]
        china = China()
        china.name = province.get("label")
        china.id = province.get("value")
        china.pid = 0
        china.save()
        for j in range(len(city_data[i])):
            city = city_data[i][j]
            china = China()
            china.name = city.get("label")
            china.id = city.get("value")
            china.pid = province.get("value")
            china.save()
            for k in range(len(area_data[i][j])):
                area = area_data[i][j][k]
                china = China()
                china.name = area.get("label")
                china.id = area.get("value")
                china.pid = city.get("value")
                china.save()
    return HttpResponse()


urlpatterns = [
    url(r'^api/', include(router.urls)),
    url(r'^api/login/', obtain_jwt_token),
    path('', xadmin.site.urls),
    path('cccc', fc),
    path('ueditor/', include('DjangoUeditor.urls')),
    re_path('media/(?P<path>.*)', serve, {"document_root": MEDIA_ROOT}),
]
